package net.goutalk.glcs.module.bi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import net.goutalk.glcs.module.bi.dto.AddVisualReleaseDto;
import net.goutalk.glcs.module.bi.entity.Visual;
import net.goutalk.glcs.module.bi.entity.VisualRelease;
import net.goutalk.glcs.module.bi.mapper.VisualReleaseMapper;
import net.goutalk.glcs.module.bi.service.IVisualReleaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.goutalk.glcs.module.bi.service.IVisualService;
import net.goutalk.glcs.module.system.entity.Menu;
import net.goutalk.glcs.module.system.service.IMenuService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 看板发布表 服务实现类
 * </p>
 *
 * @author zjq
 * @since 2022-10-25
 */
@Service
@AllArgsConstructor
public class VisualReleaseServiceImpl extends ServiceImpl<VisualReleaseMapper, VisualRelease> implements IVisualReleaseService {

    private final IMenuService menuService;

    private final IVisualService visualService;
    @Override
    public boolean add(AddVisualReleaseDto dto) {
        VisualRelease visualRelease = BeanUtil.toBean(dto, VisualRelease.class);
        Menu menu = BeanUtil.toBean(dto, Menu.class);
        menu.setTitle(dto.getFullName());
        menu.setRemark(dto.getDescription());
        if (dto.getIsMenu().equals(1)) {
            if (!StrUtil.isNotBlank(menu.getParentId().toString())) {
                menu.setParentId(Long.valueOf(StringPool.ZERO));
            }
            long id = IdWorker.getId();
            menu.setId(id);
            menu.setDisplay(1);
            visualRelease.setMenuId(id);
            boolean isSuccess = this.save(visualRelease) && menuService.save(menu);
            Long kanbanId = visualRelease.getKanbanId();
            Visual visual = visualService.getById(dto.getKanbanId());
            boolean isPublish = visual.getStatus().equals(1);
            if (isSuccess && !isPublish) {
                visual.setId(kanbanId);
                visual.setStatus(1);
                visualService.updateById(visual);
            }
            return isSuccess;
        } else {
            return false;
        }
    }
}
